/**
*  \file cosdoc.h
*
*  \brief CAN-Open SDO Client
*
*  \author   Michael J. Hohmann <michael.hohmann@linde-mh.de>
*            Michael Kunkelmann <michael.kunkelmann@linde-mh.de>
*
*
*/
#ifndef COSDOC_H_INCLUDED
#define COSDOC_H_INCLUDED

/*================================================[ public includes  ]================================================*/
#include "../cfg/prj.h"
#include "../fd/fd.h"
#include "../cosdo/cosdo_abort_codes.h"
/*================================================[ public defines   ]================================================*/

/*====================/============================[ public datatypes ]================================================*/

/** Transmit function type definition */
typedef RC(*cosdoc_transmit_function_t)(void *data, U8 msg[8]);

/** Abort function type definition */
typedef RC(*cosdoc_abort_function_t)(void *data, U32 mux, U32 code );

/* forward declaration */
struct cosdoc;

/** State function type definition */
typedef RC (*cosdoc_state_function_t)( struct cosdoc *me, U8 data[8]);

/** Status of transmission. */
enum transmission_status
{
   cosdoc_transmission_status_idle,     /**< No transmission is currently active. */
   cosdoc_transmission_status_pending,  /**< A transmission is currently pending  */
   cosdoc_transmission_status_aborted,  /**< The last transmission was aborted    */
   cosdoc_transmission_status_completed,/**< The last transmission was completed  */
   cosdoc_transmission_status_timed_out /**< The last transmission was timed out. */
};


/** Can Open SDO Client Object */
struct cosdoc
{
	cosdoc_transmit_function_t transmit;         /**< Transmit function pointer. */
	void *transmit_data;                         /** Data pointer of the transmit function. */
	cosdoc_abort_function_t abort;               /**< Abort function pointer. */
	void *abort_data;                            /**< Data pointer of the abort function. */
	cosdoc_state_function_t state;               /**< State function pointer. */
	struct fd *file;                             /**< Currently used file handle. */
	U32 current_mux;                             /**< Multiplexor of the currently requested object. */
   BO message_pending;                          /**< True if message is pending. */
   U8 incomming_message[8];                     /**< Message buffer for incoming messages. */
   enum transmission_status transmission_status;/**< Status of current transmission. */
   U32 size;                                    /**< Amount of data that are currently processed. */
   BO toggle_bit;                               /**< Expected value of the next received toggle bit. */
   U32 timeout_max;                             /**< Timeout max. */
   BO communication_timed_out;                  /**< True if timed out.*/
   BO timer_active;                             /**< True if timeout monitoring is active. */
   U32 count_down;                              /**< Number of ticks before timeout. */
};

/*================================================[ public variables ]================================================*/

/*================================================[ public functions ]================================================*/

/** Initialization
*
*  Initialization of the passed CanOpen SDO object.
*
*  \param me   Object pointer.
*  \param transmit Transmit function
*  \param callback_data Callback data for the transmit function.
*  \param abort   Abort function.
*  \param abort_data Callback data for the abort function.
*  \param timeout Number of ticks after which a timeout occurs.
*
*  \return Always RC_SUCCESS.
*/
RC cosdoc_initialize
(
   struct cosdoc *me,
   cosdoc_transmit_function_t transmit,
   void* callback_data,
   cosdoc_abort_function_t abort,
   void *abort_data,
   U32 timeout
);

/** Process incoming messages.
*
*  The function cosodc_process() process the incoming data. It is expected that this function is called from the
*  can interrupt level.
*
*  \param me Object pointer.
*  \param data Incoming message.
*/
void cosdoc_process( struct cosdoc *me, U8 data[8]);

/** Transfer data to the server.
*
*  \param me Object pointer.
*  \param mux SDO Multiplexor
*  \param size Number of bytes to be transferred.
*  \param fh File handle.
*
*  \return RC_SUCCESS.
*
*/
RC cosdoc_download( struct cosdoc *me, U32 mux, U32 size, struct fd *fh );

/** Get transmission status.
*
*  \param me Object pointer.
*  \return Status of current transmission.
*/
enum transmission_status cosdoc_get_status(struct cosdoc *me);

/** Send abort code
*
*  \param me Object pointer.
*  \param mux SDO Multiplexor.
*  \param code abort code.
*
*/
RC cosdoc_abort(struct cosdoc *me, U32 mux, U32 code);

/** Request data from server.
*
*  \param me Object pointer.
*  \param mux SDO Multiplexor.
*  \param fh File handle.
*
*  \return RC_SUCCESS.
*/

RC cosdoc_upload( struct cosdoc *me, U32 mux, struct fd *fh );

/** Timer Service
*
*  The function cosdoc_timer_service is controlling the timeout monitoring.
*
*  \param me Object pointer.
*  \param ticks Number of ticks occurred since the last call.
*
*/
void cosdoc_timer_service( struct cosdoc *me, U32 ticks );

/** Status Service function
*
*  The function cosdoc_idle_service() is controlling the internal state machine.
*  The function should be called as often as possible with lowest priority.
*
*  \param me Object pointer.
*
*/
void cosdoc_idle_service( struct cosdoc *me );

/*====================================================================================================================*/

#endif

/***********************************************************************************************************************
*                             Copyright 2010 - 2013 Linde Material Handling. All rights reserved.                      *
***********************************************************************************************************************/
